﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Abp.Auditing;
using Abp.Web.Models;
using FwProject.Com;
using FwProject.Com.Dto;
using FwProject.Configuration;
using FwProject.Controllers;
using FwProject.ShoppingMall.Com;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;

namespace FwProject.Web.Shop.Controllers
{
    /// <summary>
    /// 规格
    /// </summary>
    public class SpceController : FwProjectControllerBase
    {
        private readonly ISpceAppService _spceAppService;
        private readonly IHostingEnvironment _env;
        private readonly IConfigurationRoot _appConfiguration;

        public SpceController(ISpceAppService spceAppService, IHostingEnvironment env)
        {
            _spceAppService = spceAppService;
            _env = env;
            _appConfiguration = _env.GetAppConfiguration();
        }

        public IActionResult Index()
        {
            return View();
        }


        [HttpGet]
        [DisableAuditing]
        [WrapResult(false, false)]//关闭ABP的返回数据封装
        public async Task<IActionResult> GetSpceData(int page, int limit, string keyword = "")
        {
            var data = await _spceAppService.GetSpceLayerDataAsync(page, limit, keyword);
            return Json(data);
        }

        /// <summary>
        /// 增加规格
        /// </summary>
        /// <returns></returns>
        public IActionResult Create()
        {
            ViewBag.ImageUrl = _appConfiguration["ImageUrl:url"].ToString();
            return View();
        }

        [HttpPost]
        public async Task<IActionResult> Create(SpceEnterDto input)
        {
            var id = await _spceAppService.CreateAsync(input);
            return Json(id);
        }

        /// <summary>
        /// 修改规格
        /// </summary>
        /// <param name="id">规格Id</param>
        /// <returns></returns>
        public async Task<IActionResult> Edit(Guid id)
        {
            ViewBag.ImageUrl = _appConfiguration["ImageUrl:url"].ToString();
            ViewBag.Id = id;
            var entity = await _spceAppService.GetEnterDtoAsync(id);
            return View(entity);
        }

        [HttpPost]
        public async Task<IActionResult> Edit(Guid id, SpceEnterDto input)
        {
           var result= await _spceAppService.ModifyAsync(id, input);
            return Json(result);
        }


        public async Task<IActionResult> Delete(Guid id)
        {
            await _spceAppService.DeleteAsync(id);
            return Json(id);
        }

        /// <summary>
        /// 上传图片
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UploadImage()
        {
            string FileServicePath = _appConfiguration["FileServicePath:TempSpce"].ToString();
            return Json(base.UploadFile(FileServicePath));
        }
    }
}